/** 
 * Project Name:sys-portal-Extjs 
 * File Name:MainCtrl.js 
 * Package Name: 
 * Author : 吴Yuan|小森森
 * Date:2018年4月13日下午4:50:35 
 * Copyright (c) 2018, 15909910367@163.com All Rights Reserved. 
 * 
 */
Ext.define('Jason.view.protal.module.systemSetting.dataSource.MainCtrl', {
    extend: 'Wys.basic.BaseCtrl',
    alias: 'controller.dataSouceCtrl',
    formItems: function () {
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            collapsible: false,
            defaultType: 'textfield',
            layout: {
                type: 'table',
                columns: 2
            },
            defaults: {
                width: '100%',
                labelWidth: 80
            },
            items: [
                { fieldLabel: 'id', name: 'id', xtype: 'textfield', value: '', hidden: true },
                { fieldLabel: '数据源名称', name: 'title', xtype: 'textfield', allowBlank: false, emptyText: '请输入数据源名称' },
                { fieldLabel: '顺序号', name: 'orderNo', xtype: 'numberfield', value: 1, maxValue: 999, minValue: 1 },
                {
                    xtype: 'RemoteCombobox',
                    fieldLabel: '数据库类型',
                    name: 'dataBaseType',
                    emptyText: '请选择数据库类型...',
                    model: Ext.create('Ext.data.Model', {
                        fields: ['key', 'value']
                    }),
                    valueField: 'value',
                    displayField: 'key',
                    proxy_url: reqUrl.sys.comboData.dict + "DBType",
                    allowBlank: false,
                    listeners: {
                        select: function (combo, record) {
                            var formPanel = combo.up('baseForm');
                            if (combo.getValue() != 'FTP') {
                                formPanel.down('#driverClassName').setValue(record.get('remarks'));
                                formPanel.down('#portNumber').setValue(record.get('keycode'));
                                formPanel.down('#driverClassName').show();
                                formPanel.down('#dbName').show();
                                formPanel.down('#otherStr').show();
                                formPanel.down('#driverClassName').allowBlank = false;
                                formPanel.down('#dbName').allowBlank = false;
                            } else {
                                formPanel.down('#driverClassName').hide();
                                formPanel.down('#driverClassName').allowBlank = true;
                                formPanel.down('#dbName').allowBlank = true;
                                formPanel.down('#dbName').hide();
                                formPanel.down('#otherStr').hide();
                            }
                        },
                        render: function (combo) {
                            if (combo.getValue()) {
                                var formPanel = combo.up('baseForm');
                                if (combo.getValue() != 'FTP') {
                                    formPanel.down('#driverClassName').show();
                                    formPanel.down('#dbName').show();
                                    formPanel.down('#otherStr').show();
                                    formPanel.down('#driverClassName').allowBlank = false;
                                    formPanel.down('#dbName').allowBlank = false;
                                } else {
                                    formPanel.down('#driverClassName').hide();
                                    formPanel.down('#dbName').hide();
                                    formPanel.down('#otherStr').hide();
                                    formPanel.down('#driverClassName').allowBlank = true;
                                    formPanel.down('#dbName').allowBlank = true;
                                }
                            }
                        }
                    }
                },
                { fieldLabel: '驱动类名', name: 'driverClassName', itemId: 'driverClassName', xtype: 'textfield', readOnly: true },
                { fieldLabel: 'IP地址', name: 'ipAddress', xtype: 'textfield', allowBlank: false, emptyText: '127.0.0.1' },
                { fieldLabel: '端口号', name: 'portNumber', itemId: 'portNumber', xtype: 'textfield', allowBlank: false, maxLength: 5, emptyText: '0000' },
                { fieldLabel: '数据库名称', name: 'defaultSchema', xtype: 'textfield', allowBlank: false, itemId: 'dbName' },
                { fieldLabel: '连接附加串', name: 'urlAddition', xtype: 'textfield', itemId: 'otherStr' },
                { fieldLabel: '用户名', name: 'userName', xtype: 'textfield', allowBlank: false },
                { fieldLabel: '连接密码', name: 'password', xtype: 'textfield', allowBlank: false },
                { fieldLabel: '节点名称', name: 'useName', xtype: 'textfield', allowBlank: false },
                {
                    fieldLabel: '启用', name: 'disabled', xtype: 'RemoteCombobox',
                    valueField: 'value',
                    displayField: 'key',
                    value: 1,
                    proxy_url: reqUrl.sys.comboData.dict + "YESNO"
                },
                { fieldLabel: '数据源说明', name: 'dbRemark', graw: true, colspan: 2, xtype: 'textarea', maxLength: 250, emptyText: '选填' }/*,
					{fieldLabel:'连接说明',name:'linkRemark',graw:true,colspan:2,xtype:'textarea',maxLength:250,emptyText:'选填',itemId:'linkRemark'}*/
            ]
        }];
    },
    onNewButtonClick: function (btn) {
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 800,
            title: '数据源管理',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemSetting.DataSource',
                gridPanel: btn.up('#dataSourceManagerView'),
                items: me.formItems(),
                moreButton: [{
                    text: '测试连接',
                    ui: 'button-linkbtn',
                    iconCls: 'fa-exchange',
                    handler: function (btn) {
                        EUtil.RS({
                            url: reqUrl.systemSetting.dataSouce.test,
                            params: btn.up('form').getValues(),
                            msg: true,
                            callback: function (resp) {
                                if (resp.success) {
                                    EUtil.showSuccess('数据源连接成功');
                                } else {
                                    EUtil.showWarning(Ext.String.format(I18N.FailedOtherCode, resp.code, resp.msg));
                                }
                            }
                        })
                    }
                }]
            }
        }).show();
    },
    onNewWithCopyButtonClick: function (btn) {
        var me = this;
        var records = me.getView().getSelection(), record = null;
        if (records.length === 0 || records.length > 1) {
            EUtil.toastWarn('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
            return;
        } else {
            record = records[0];
            record.setId(null);
        }
        Ext.create('Wys.form.FormWindow', {
            width: 800,
            title: '数据源管理',
            config: {
                operatetype: 'new',
                gridPanel: me.getView(),
                model: 'Jason.model.protal.module.systemSetting.DataSource',
                items: me.formItems(),
                formData: record
            },
            moreButton: [{
                text: '测试连接',
                ui: 'button-linkbtn',
                iconCls: 'fa-exchange',
                handler: function (btn) {
                    EUtil.RS({
                        url: reqUrl.systemSetting.dataSouce.test,
                        params: btn.up('form').getValues(),
                        msg: true,
                        callback: function (resp) {
                            if (resp.success) {
                                EUtil.showSuccess('数据源连接成功');
                            } else {
                                EUtil.showWarning(Ext.String.format(I18N.FailedOtherCode, resp.code, resp.msg));
                            }
                        }
                    })
                }
            }]
        }).show();
    },
    onPreviewButtonClick: function (grid, record) {
        var me = this;
        if (grid.xtype.indexOf('button') != -1) {
            records = grid.up('baseView').getSelection();
            grid = grid.up('baseView');
            if (records.length === 0 || records.length > 1) {
                EUtil.showWarning('请选择要查看的数据。');
                return;
            } else {
                record = records[0];
            }
        }
        Ext.create('Wys.form.FormWindow', {
            width: 800,
            title: ' 『' + record.get('title') + '』数据源 信息',
            config: {
                model: 'Jason.model.protal.module.systemSetting.DataSource',
                operatetype: 'display',
                gridPanel: grid,
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onEditButtonClick: function (grid, record) {
        var me = this;
        if (grid.xtype.indexOf('button') != -1) {
            records = grid.up('baseView').getSelection();
            grid = grid.up('baseView');
            if (records.length === 0 || records.length > 1) {
                EUtil.toastWarn('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
                return;
            } else {
                record = records[0];
            }
        }
        Ext.create('Wys.form.FormWindow', {
            width: 800,
            title: ' 『' + record.get('title') + '』数据源 信息',
            config: {
                model: 'Jason.model.protal.module.systemSetting.DataSource',
                operatetype: 'edit',
                gridPanel: grid,
                items: me.formItems(),
                formData: record,
                moreButton: [{
                    text: '测试连接',
                    ui: 'button-linkbtn',
                    iconCls: 'fa-exchange',
                    handler: function (btn) {
                        EUtil.RS({
                            url: reqUrl.systemSetting.dataSouce.test,
                            params: btn.up('form').getValues(),
                            msg: true,
                            callback: function (resp) {
                                if (resp.success) {
                                    EUtil.showSuccess('数据源连接成功');
                                } else {
                                    EUtil.showWarning(Ext.String.format(I18N.FailedOtherCode, resp.code, resp.msg));
                                }
                            }
                        })
                    }
                }]
            }
        }).show();
    },
    onDeleteButtonClick: function (btn) {
        var grid = btn.up('#dataSourceManagerView'),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelection(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.showWarning('请选择要操作的数据');
            return;
        }
        Ext.Array.each(selectGridData, function (item) {
            removeName.push(item.data.title);
            removePK.push(item.data.id);
        });
        EUtil.showMsg({
            option: 3,
            title: '温馨提示',
            message: '您确定删除<span style="color:red;">『' + removeName.join() + '』</span>吗？',
            callback: function (btn) {
                if (btn === 'ok') {
                    gridStore.remove(selectGridData);
                    gridStore.sync({
                        callback: function () {
                            gridStore.reload();
                        }
                    });
                }
            }
        });
    }
});